package library.and.xps.com.step.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;

import java.util.ArrayList;
import java.util.List;

import library.and.xps.com.step.R;
import library.and.xps.com.step.m.model.ActiveMemberRanking;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.view.customview.CBProgressBar;

public class TeamActiveMemberAdapter extends ArrayAdapter {
    private List<ActiveMemberRanking.ListBean> ranking = new ArrayList<>();
    private LayoutInflater layoutInflater = null;
    private Context context;
    private int exercise;
    private int type = 0;
    private int format = 0;

    public TeamActiveMemberAdapter(Context context) {
        super(context, R.layout.active_member_item);
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
        notifyDataSetChanged();
    }


    public void addData(List<ActiveMemberRanking.ListBean> ranking, int exercise) {
        this.exercise = exercise;
        this.ranking = ranking;
        notifyDataSetChanged();
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setFormat(int format) {
        this.format = format;
    }

    @Override
    public int getCount() {
        return ranking.size();
    }

    @Override
    public Object getItem(int position) {
        return ranking.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        View view = convertView;
        ActiveMemberRanking.ListBean listBean = ranking.get(position);
        if (view == null) {
            view = layoutInflater.inflate(R.layout.active_member_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tv_member_ranking = (TextView) view.findViewById(R.id.tv_member_ranking);
            viewHolder.iv_step = (TextView) view.findViewById(R.id.tv_member_step);
            viewHolder.iv_header = (ImageView) view.findViewById(R.id.iv_member_header);
            viewHolder.tv_name = (TextView) view.findViewById(R.id.tv_name);
            viewHolder.cb_1 = (CBProgressBar) view.findViewById(R.id.pb_1);
            viewHolder.tv_format = (TextView) view.findViewById(R.id.tv_format);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (exercise != 0) {
            viewHolder.cb_1.setMax(exercise);
        } else {
            viewHolder.cb_1.setMax(10000);
        }

        viewHolder.cb_1.setProgress(listBean.getUsexercise());
        viewHolder.tv_name.setText(listBean.getNickName()+"");
        viewHolder.iv_step.setText(listBean.getUsexercise()+"");
        viewHolder.tv_member_ranking.setText(position + 1 + "");
        CommonUtils.setUpimg(context, viewHolder.iv_header, listBean.getIco(), true);
        if (type == 1) {
            viewHolder.tv_format.setText("步");
        } else if (type == 2) {
            viewHolder.tv_format.setText("公里");
        }
        return view;
    }

    static class ViewHolder {
        TextView iv_step, tv_member_ranking, tv_name;
        ImageView iv_header;
        CBProgressBar cb_1;
        TextView tv_format;
    }
}
